package system

import (
	"context"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/system"
	systemReq "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
)

type Protocol_managementService struct{}

// CreateProtocol_management 创建协议管理记录
// Author [yourname](https://github.com/yourname)
func (protocol_managementService *Protocol_managementService) CreateProtocol_management(ctx context.Context, protocol_management *system.Protocol_management) (err error) {
	err = global.GVA_DB.Create(protocol_management).Error
	return err
}

// DeleteProtocol_management 删除协议管理记录
// Author [yourname](https://github.com/yourname)
func (protocol_managementService *Protocol_managementService) DeleteProtocol_management(ctx context.Context, ID string) (err error) {
	err = global.GVA_DB.Delete(&system.Protocol_management{}, "id = ?", ID).Error
	return err
}

// DeleteProtocol_managementByIds 批量删除协议管理记录
// Author [yourname](https://github.com/yourname)
func (protocol_managementService *Protocol_managementService) DeleteProtocol_managementByIds(ctx context.Context, IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]system.Protocol_management{}, "id in ?", IDs).Error
	return err
}

// UpdateProtocol_management 更新协议管理记录
// Author [yourname](https://github.com/yourname)
func (protocol_managementService *Protocol_managementService) UpdateProtocol_management(ctx context.Context, protocol_management system.Protocol_management) (err error) {
	err = global.GVA_DB.Model(&system.Protocol_management{}).Where("id = ?", protocol_management.ID).Updates(&protocol_management).Error
	return err
}

// GetProtocol_management 根据ID获取协议管理记录
// Author [yourname](https://github.com/yourname)
func (protocol_managementService *Protocol_managementService) GetProtocol_management(ctx context.Context, ID string) (protocol_management system.Protocol_management, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&protocol_management).Error
	return
}

// GetProtocol_managementInfoList 分页获取协议管理记录
// Author [yourname](https://github.com/yourname)
func (protocol_managementService *Protocol_managementService) GetProtocol_managementInfoList(ctx context.Context, info systemReq.Protocol_managementSearch) (list []system.Protocol_management, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&system.Protocol_management{})
	var protocol_managements []system.Protocol_management
	// 如果有条件搜索 下方会自动创建搜索语句
	if len(info.CreatedAtRange) == 2 {
		db = db.Where("created_at BETWEEN ? AND ?", info.CreatedAtRange[0], info.CreatedAtRange[1])
	}

	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&protocol_managements).Error
	return protocol_managements, total, err
}
func (protocol_managementService *Protocol_managementService) GetProtocol_managementPublic(ctx context.Context) {
	// 此方法为获取数据源定义的数据
	// 请自行实现
}
